<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="libra.util.js"></script>
<script type="text/javascript" src="libra.controller.js"></script>
<script type="text/javascript" src="libra.model.js"></script>
<script type="text/javascript" src="libra.model.promise.js"></script>
<script type="text/javascript" src="libra.model.promise.request.js"></script>
</head>
<body>

<h3>Convert REQUEST_TIME / REQUEST_TIME as Date</h3>
<div id="view"></div>

<script type="text/javascript">

var util = '$$$util$$$' in this ? this['$$$util$$$'] : require('./libra.util');
var ModelRequest = util.using('model.promise.request');
var Controller = util.using('controller');

var m = ModelRequest.create(function(v, k){
	switch(k){
		case 'text':
		case 'error':
			return v || null;
		case 'status':
			if(util.isValidNumber(v)) return v;
			return 0;
		default:
			return v;
	}
});

var sch = new util.DataSchema({
	REQUEST_TIME: 'date', REQUEST_TIME_FLOAT: 'date'
});


var c = Controller.create(function(target, val, oldVal, model, targetId){
	if(!val || val.status <= 0){
		document.querySelector('#view').innerHTML = 'Loading...';
		return;
	} else if(!val.error && val.text) {
		val = JSON.parse(val.text);
		var html = ['<ul>'];
		util.each(val, function(v, p){
			html.push(util.format('<li><label>{0}:</label> <span>{{0}}<span></li>', p, v));
		});
		html.push('</ul>');
		html.push('<button onclick="request()">Update</button>');
		var temp = html.join('');
		var s = util.format(temp, sch.convert(val));
		setTimeout(function(){
			document.querySelector('#view').innerHTML = s;
		}, 1000);
	}
});

m.bind(c);

var url = 'http://127.0.0.1/git/libra/server-info.php';
function request(){
	m.reset().request({ url: url });
}
request();


</script>
</body>
</html>